package com.jeesite.modules.isys.xmqtt.client;

import com.jeesite.modules.isys.xmqtt.hander.ConnectionHander;
import com.jeesite.modules.isys.xmqtt.hander.XHander;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.List;


@Component
@Slf4j
public class XMqttCallback implements MqttCallback {

    @Autowired
    private ConnectionHander connectionHander;

    @Override
    public void connectionLost(Throwable throwable) {
        log.error(throwable.getMessage());
        connectionHander.hander();
    }



    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        List<XHander> handerList = XSubscriptions.getHanderList(s);
        if (handerList!=null){
            for (XHander hander : handerList){
                mqttMessage =  hander.hander(mqttMessage);
            }
        }

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}
